package com.qiqv;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.Arrays;

public class MPGenertorBean {
    public static void main(String[] args) {
        // 构建一个代码生成器对象
        AutoGenerator autoGenerator = new AutoGenerator();

        // 配置全局策略
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/mybatis-plus-generator/src/main/java");
        gc.setAuthor("moutory"); //配置作者信息
        gc.setOpen(false); // 是否打开输出目录
        gc.setFileOverride(false); // 是否覆盖
        gc.setServiceName("%sService"); // 去Service的I前缀
        gc.setIdType(IdType.ID_WORKER); //设置id自增策略
        gc.setDateType(DateType.ONLY_DATE);
        gc.setSwagger2(true); //是否生成swagger
        autoGenerator.setGlobalConfig(gc);

        // 配置数据源
        DataSourceConfig dsConfig = new DataSourceConfig();
        dsConfig.setUrl("jdbc:mysql://localhost:3306/mybatis-plus");
        dsConfig.setUsername("root");
        dsConfig.setPassword("root");
        dsConfig.setDriverName("com.mysql.jdbc.Driver");
        dsConfig.setDbType(DbType.MYSQL);
        autoGenerator.setDataSource(dsConfig);

        //配置包
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("demo");
        pc.setParent("com.qiqv");
        pc.setController("controller");
        pc.setMapper("mapper");
        pc.setEntity("pojo");
        pc.setService("service");
        autoGenerator.setPackageInfo(pc);

        // 配置策略
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("user"); // 设置要映射的表名，可以一次传多个
        strategyConfig.setNaming(NamingStrategy.underline_to_camel); //数据库表映射到实体的命名策略:下划线转驼峰命名
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
        strategyConfig.setEntityLombokModel(true); // 自动Lombok
        strategyConfig.setLogicDeleteFieldName("deleted");
            //自动填充设置
        TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
        TableFill gmtModified = new TableFill("update_time",FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtModified);
        strategyConfig.setTableFillList(tableFills);
            //配置乐观锁
        strategyConfig.setVersionFieldName("version");
        strategyConfig.setRestControllerStyle(true);
        strategyConfig.setControllerMappingHyphenStyle(true);//指定rest请求风格
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.execute();

    }
}
